function GetQueryString(name) {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null) {
        return  unescape(r[2]);
    } else {
         return null;
   }      
}
var playerId= GetQueryString("id");//球员id
var gameType = GetQueryString("type");//比赛类型


function getApi(options){
    return new Promise(function(resolve,reject){
        $.ajax({
            type:"get",
            url:options.url,
            success:function(data){
                resolve(data)
            },
            error:function(err){
                reject(err)
            }
        });
    })
}

//Promise的all方法，等数组中的所有promise对象都完成执行
if(gameType && playerId){
    // http://127.0.0.1:8080?id=3&type=2
    var urlbaseInfo = "http://127.0.0.1:8080/mock/playerinfo"+playerId+".json";
    var urlbaseGame = "http://127.0.0.1:8080/mock/player"+gameType+".json";
    Promise.all(
        [
        getApi({url:urlbaseInfo}),
        getApi({url:urlbaseGame})
    ]).then(function([userInfo,playgame]){
        successUserInfo(userInfo);
        successPlayGame(playgame)
    },function(){
        fallCallback()
    })
}else{
    console.log("默认页面")
}
function fallCallback(){
    console.log("接口请求失败")
}
function successUserInfo(userInfo){
    if(userInfo&&userInfo.state=="S_OK"){
        var res = userInfo.data;
        $("#playerName").text(res.name);
        $("#playtime").text(timeTranlate(res.time))
    }
}
function successPlayGame(playgame){
    if(playgame&&playgame.state=="S_OK"){
        var res = playgame.data;
        $("#playerType").text(res.type);
        $("#gameScroe").text(res.scroe);
        $("#gameLeve").text(res.gameLeve);
        
        $("#usetime").text(res.usetime);
        $("#rate").text(res.rate);
        $("#rotate").text(res.rotate);
        $("#fluency").text(res.fluency);
        $("#floor").text(res.floor);
        Renderstar(res.star);
        $("#ranking").text(res.ranking)
    }else{
        console.log("接口数据异常")
    }

}
function timeTranlate(time){
    return new Date(time).toLocaleString()
}

function Renderstar(star){
    $("#starCon .share-score-star").each(function(index,item){
        if(star>index){
            $(this).attr({src:"img/share/ic_star.png"})
        }
    })

}